using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Magiq.Behaviors;
using Magiq.Insert;
using Magiq.Support;
using Magiq.Update;
using MirrorMirror;

namespace Magiq.Providers.Objects
{
    public class DefaultObjectsBehavior : DefaultBehavior, IObjectsDeleteBehavior, IObjectsInsertBehavior,
                                           IObjectsUpdateBehavior
    {
        #region IObjectsDeleteBehavior Members

        IList IObjectsDeleteBehavior.FindSourceOf<T>(IEnumerable<T> collection)
        {
            return new EnumerableSource(collection).List;
        }

        int IObjectsDeleteBehavior.RemoveItems<T>(IEnumerable<T> itemsToRemove, IList source)
        {
            var count = 0;
            foreach (var item in itemsToRemove.ToArray())
            {
                source.Remove(item);
                count++;
            }
            return count;
        }

        #endregion

        #region IObjectsInsertBehavior Members

        public IInsertDestination<TNew> GetInsertDestination<TSource, TDestination, TNew>(
            IInsert<TSource, TDestination, TNew> insert)
            where TDestination : class
            where TNew : class, TDestination, new()
        {
            return Magiq.ForInsertDestination(insert);
        }

        #endregion

        #region IObjectsUpdateBehavior Members

        public object GetValue(object item, IPropertySet set, Delegate dictionary)
        {
            return dictionary.DynamicInvoke(item);
        }

        public bool ShouldUpdateWhenValueIsNull<T>(T item, IPropertySet set)
        {
            return false;
        }

        public object ValueForNull<T>(T item, IPropertySet set)
        {
            return null;
        }

        public void SetProperty<T>(T itemToUpdate, string property, object value)
        {
            itemToUpdate.Set(property, value);
        }

        #endregion
    }
}